package com.lynn.common.dialog

import android.content.Context
import android.view.View

/**
 * @description:
 * @author: bailing
 * @datetime: 2025/5/29 11:13
 **/
class CustomAlertDialog {

    class Builder(context: Context) : BaseDialog.Builder(context, R.layout.dialog_alert) {
        private var mTitle: String? = null
        private var mContent: String? = null
        private var mShowTitle = true
        private var mOnClickListener: OnClickListener? = null

        // Override the convertView method to set the width, horizontal gap, cancelable and canceled on touch outside properties of the dialog
        override fun convertView(viewHolder: ViewHolder, dialog: BaseDialog) {
            // Set the width of the dialog to the screen width of the context
            setWidth(getScreenWidth(context))
            // Set the horizontal gap of the dialog to 50dp
            setHorizontalGap(dp2px(context, 50f).toInt())

            // Set the dialog to be cancelable
            setCancelable(false)
            // Set the dialog to be canceled on touch outside
            setCanceledOnTouchOutside(false)

            // Set the text of the title TextView in the dialog to the value of mTitle
            viewHolder.setText(R.id.tv_title, mTitle!!)
            // Set the text of the content TextView in the dialog to the value of mContent
            viewHolder.setText(R.id.tv_content, mContent!!)
            // Set an OnClickListener for the cancel TextView in the dialog
            viewHolder.setOnClickListener(R.id.tv_cancel, object : View.OnClickListener {
                override fun onClick(v: View?) {
                    // Call the onCancel method of the OnClickListener
                    mOnClickListener?.onCancel()
                    // Dismiss the dialog
                    dialog.dismiss()
                }
            })
            // Set an OnClickListener for the sure TextView in the dialog
            viewHolder.setOnClickListener(R.id.tv_sure) {
                // Call the onSure method of the OnClickListener
                mOnClickListener?.onSure()
                // Dismiss the dialog
                dialog.dismiss()
            }
        }

        fun setTitle(title: String): Builder {
            mTitle = title
            return this
        }
        fun setShowTitle(show: Boolean): Builder {
             mShowTitle = show
            return this
        }

        fun setContent(content: String): Builder {
            mContent = content
            return this
        }

        fun setOnClickListener(listener: OnClickListener): Builder {
            mOnClickListener = listener
            return this
        }
    }

    interface OnClickListener {
        fun onSure()
        fun onCancel()
    }
}
